
#import "LoadMoreTableFooterView.h"

#define TEXT_COLOR	 [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0]
#define FLIP_ANIMATION_DURATION 0.18f

@interface LoadMoreTableFooterView (Private)
- (void)setState:(LoadMoreState)aState;
@end

@implementation LoadMoreTableFooterView

@synthesize textLoading, textPullUpToLoad, textReleaseToLoad;

@synthesize delegate=_delegate;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
		self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.backgroundColor = [UIColor clearColor];
		//self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];
		
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 20.0f, self.frame.size.width, 20.0f)];
		label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		label.font = [UIFont boldSystemFontOfSize:15.0f];
		label.textColor = TEXT_COLOR;
		label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
		label.shadowOffset = CGSizeMake(0.0f, 1.0f);
		label.backgroundColor = [UIColor clearColor];
		label.textAlignment = NSTextAlignmentCenter;
		[self addSubview:label];
		_statusLabel=label;
				
		UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
		view.frame = CGRectMake(150.0f, 20.0f, 20.0f, 20.0f);
		[self addSubview:view];
		_activityView = view;
		self.hidden = YES;
		
		[self setState:LoadMoreNormal];
    }
    return self;	
}


#pragma mark -
#pragma mark Setters

- (void)setState:(LoadMoreState)aState{	
	switch (aState) {
		case LoadMorePulling:
            if ([self.textReleaseToLoad length] > 0) {
                _statusLabel.text = self.textReleaseToLoad;
            }
            else {
                _statusLabel.text = NSLocalizedString(@"松开载入更多", @"松开载入更多...");
            }
			break;
		case LoadMoreNormal:
            if ([self.textPullUpToLoad length] > 0) {
                _statusLabel.text = self.textPullUpToLoad;
            }
            else {
                _statusLabel.text = NSLocalizedString(@"上拉载入更多", @"载入更多...");
            }
			_statusLabel.hidden = NO;
			[_activityView stopAnimating];
			break;
		case LoadMoreLoading:
			_statusLabel.hidden = YES;
			[_activityView startAnimating];
			break;
		default:
			break;
	}
	
	_state = aState;
}


#pragma mark -
#pragma mark ScrollView Methods

- (void)loadMoreScrollViewDidScroll:(UIScrollView *)scrollView {
	if (_state == LoadMoreLoading) {
		scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 60.0f, 0.0f);
	}
    else if (scrollView.isDragging && (scrollView.contentSize.height > scrollView.frame.size.height)) {
        
        CGFloat position = (scrollView.contentSize.height - scrollView.frame.size.height - scrollView.contentOffset.y) * -1;
		
        
        BOOL _loading = NO;
		if ([_delegate respondsToSelector:@selector(loadMoreTableFooterDataSourceIsLoading:)]) {
			_loading = [_delegate loadMoreTableFooterDataSourceIsLoading:self];
		}
		
        //NSLog(@"scrollView.contentSize.height %f, scrollView.contentOffset.y %f", scrollView.contentSize.height, scrollView.contentOffset.y);
        
		/*if (_state == LoadMoreNormal && scrollView.contentOffset.y < (scrollView.contentSize.height - 350) && scrollView.contentOffset.y > (scrollView.contentSize.height - 410) && !_loading) {
			self.frame = CGRectMake(0, scrollView.contentSize.height, self.frame.size.width, self.frame.size.height);
			self.hidden = NO;
		} else if (_state == LoadMoreNormal && scrollView.contentOffset.y > (scrollView.contentSize.height - 350) && !_loading) {
			[self setState:LoadMorePulling];
		} else if (_state == LoadMorePulling && scrollView.contentOffset.y < (scrollView.contentSize.height - 350) && scrollView.contentOffset.y > (scrollView.contentSize.height - 410) && !_loading) {
			[self setState:LoadMoreNormal];
		}*/
        
        self.frame = CGRectMake(0, scrollView.contentSize.height, self.frame.size.width, self.frame.size.height);
        self.hidden = NO;
        
        if (_state == LoadMoreNormal && position > 60 && !_loading) {
			[self setState:LoadMorePulling];
		}
        else if (_state == LoadMorePulling && position > 0 && position < 30 && !_loading) {
            [self setState:LoadMoreNormal];
        }
        
		if (scrollView.contentInset.bottom != 0) {
			scrollView.contentInset = UIEdgeInsetsZero;
		}
	}
}

- (void)loadMoreScrollViewDidEndDragging:(UIScrollView *)scrollView {
	
	BOOL _loading = NO;
	if ([_delegate respondsToSelector:@selector(loadMoreTableFooterDataSourceIsLoading:)]) {
		_loading = [_delegate loadMoreTableFooterDataSourceIsLoading:self];
	}
	
    CGFloat position = (scrollView.contentSize.height - scrollView.frame.size.height - scrollView.contentOffset.y) * -1;
    
	if ( position > 30 && !_loading) {
		if ([_delegate respondsToSelector:@selector(loadMoreTableFooterDidTriggerRefresh:)]) {
			[_delegate loadMoreTableFooterDidTriggerRefresh:self];
		}
		
		[self setState:LoadMoreLoading];
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.2];
		scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 60.0f, 0.0f);
		[UIView commitAnimations];
	}
}

- (void)loadMoreScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:.3];
	[scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
	[UIView commitAnimations];
	
	[self setState:LoadMoreNormal];
	self.hidden = YES;
}

@end